class TimetableStatistic < ApplicationRecord
  class << self
    def statistic
      (Date.parse('2018-01-01')..Date.current.yesterday).each {|current_day|
        timetable = Timetable.select(
            :id,
            'companies.name as company_name',
            'stores.name as store_name',
            'coaches.name as coach_name',
            'courses.name as course_name',
            :class_start_time,
            :price,
            :place,
            :appointed_member,
        ).joins(:company, :store, :coach, :course).where(class_start_time: current_day.yesterday..current_day)
        timetable.each {|t|
          timetable_statistic = TimetableStatistic.new
          timetable_statistic.timetable_id = t.id
          timetable_statistic.company = t.company_name
          timetable_statistic.store = t.store_name
          timetable_statistic.coach = t.coach_name
          timetable_statistic.course = t.course_name
          timetable_statistic.class_start_time = t.class_start_time
          timetable_statistic.price = t.price
          timetable_statistic.place = t.place.to_i
          timetable_statistic.appointed = t.appointed_member.size
          timetable_statistic.signed = t.appointments.signed.count
          timetable_statistic.save
        }

      }
    end
  end
end
